package cn.wgx.module.ent.service;

import cn.wgx.common.base.IBaseService;
import cn.wgx.common.entity.ent.DTOEntUser;
import cn.wgx.common.entity.ent.EntUser;
import cn.wgx.common.entity.ent.Enterprise;
import cn.wgx.common.entity.sys.SysUser;
import cn.wgx.common.security.feign.module.sysuser.SysUserFeign;
import cn.wgx.common.util.StringUtils;
import cn.wgx.module.ent.mapper.EntUserMapper;
import cn.wgx.module.sys.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class EntUserService extends IBaseService<EntUserMapper, EntUser> {
    @Autowired
    SysUserFeign sysUserFeign;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    UserService userService;

    @Transactional(readOnly = true)
    public List<DTOEntUser> getEntUsers(Page page, QueryWrapper<EntUser> queryWrapper, String nickName) {
        if (StringUtils.isNotBlank(nickName)) {
//            queryWrapper.inSql("user_id","select user_id from sys_user where nickName like %" + nickName + "%");
            // apply 防止SQL注入
            queryWrapper.apply(" user_id in (select user_id from sys_user where nickName like {0})", "%" + nickName + "%");
        }
        IPage<EntUser> e = getBaseMapper().selectPage(page, queryWrapper);
        List<Integer> integerIdList = getIntegerIdList(e.getRecords(), "user_id");
        List<SysUser> usersBySysUserIds = sysUserFeign.findUsersByUserIds(integerIdList);
        List<DTOEntUser> dtoEntUsers = new ArrayList<>();
        for (EntUser entUser : e.getRecords()) {
            DTOEntUser dtoEntUser = new DTOEntUser();
            BeanUtils.copyProperties(entUser, dtoEntUser);
            for (SysUser sysUser : usersBySysUserIds) {
                if (entUser.getUser_id().equals(sysUser.getUser_id())) {
                    BeanUtils.copyProperties(sysUser, dtoEntUser);
                }
            }
            Enterprise byCode = enterpriseService.getByCode(entUser.getEntCode());
            if (byCode != null) {
                dtoEntUser.setEntname(byCode.getName());
            }
            dtoEntUsers.add(dtoEntUser);
        }
        return dtoEntUsers;
    }
    @Transactional(readOnly = true)
    public List<SysUser> getNotInEntUsers(Page page, Integer entId, String nickName) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper();
        queryWrapper.apply(" user_id not in (select user_id from ent_user where ent_id = {0}) ", entId);
        if(StringUtils.isNotBlank(nickName)){
            queryWrapper.like("nickname", nickName);
        }
        Page page1 = userService.page(page, queryWrapper);
        return page1.getRecords();
    }
}
